/**
 * 弯弯工作组
 */
function friendsreferralInit(url) {
	$("#hidTeamId").hide();
	$.post(url + "friendsreferral/getFridensList", {
		"userId" : "2",
		"uncheckTeamId" : -2
	}, function(data) {
		var source = data;
		$('#jqxTree').jqxTree({
			height : '350px',
			source : source,
			width : '147px',
			theme : 'summer',
			allowDrag:false
		});
		getReferralTeam(url,{"id":-2});
		$('#jqxTree').on('expand', function (event) {
			var htmlElement = event.args.element;			
			var item = $('#jqxTree').jqxTree('getItem', htmlElement);
			if (item.value=="team") {
				getReferralTeam(url,item);
				//$("#friendTip").html('请选择' + item.label + '群下推荐');

			} else {
				
			}
 
		});
		
		
		$('#jqxTree').on('select', function (event) {
			var htmlElement = event.args.element;			
			var item = $('#jqxTree').jqxTree('getItem', htmlElement);
			if (item.value == "team") {
				
			} else {
				$("#friendTip").html($("#friendTip").html() + '成员' + item.label);
				$("#hidIsReferralId").val(item.value);
			
			}
		}); 

		
	}, "json");

	$("#referralSend").click(function(){
		
		 var items = $('#teamjqxTree').jqxTree('getCheckedItems');
         var checkedItems = new Array();
         $.each(items, function () {                   
             checkedItems[checkedItems.length] = this.value;
         });
         
         var result="";
         for(var i=0; i<checkedItems.length; i++)
         {
             result+=checkedItems[i]=="team"?"":checkedItems[i];
	         if(i!=checkedItems.length-1&&checkedItems[i]!="team")
	             result+=",";
         }
         if(result==""){
        	alert("请选择推荐人"); 
        	return;
         }
         $("#hidFriendId").val(result);
		
		$('#submitForm').ajaxSubmit({
			url : url+"/friendsreferral/addFriends",  
			//dataType : "json",  
			type:"POST",
            success : function(data){ 
            	if(data==0){
            		alert("您的好友引荐失败，因为他们认识");
            	}
				alert();		              
            },  
            error:function(){  
               alert('error');  
           }   
		}); 
	});
	// Create jqxTree.
	// $('#jqxTree').jqxTree({ theme: 'summer', source: source, height: '300px',
	// width: '300px' });

}

function getReferralTeam(url,item){	
	$.post(url + "friendsreferral/getFridensList", {
		"userId" : "2",
		"uncheckTeamId" :item.id
	}, function(data) {
		var source = data;
		$('#teamjqxTree').jqxTree({
			height : '350px',
			source : source,
			checkboxes : true,
			hasThreeStates: true,
			width : '147px',
			theme : 'summer',
			allowDrag:false
		});
		/*$('#teamjqxTree').on('checkChange', function(event) {
			var args = event.args;
			var element = args.element;
			var checked = args.checked;
			var item = $('#teamjqxTree').jqxTree('getItem', element);
			if (item.id == 1) {
				$('#teamjqxTree').jqxTree('uncheckItem', element);
			} else {

			}

		});*/

		$('#teamjqxTree').bind(
				'select',
				function(event) {
					var htmlElement = event.args.element;
					alert($("#teamjqxTree").val());
					var item = $('#teamjqxTree').jqxTree('getItem', htmlElement);
					if (item.id == 1) {
						$('#teamjqxTree').jqxTree('uncheckItem', htmlElement);
					} else {
						$("#friendTip").html($("#friendTip").html() + ' 推荐给' + item.label);
						$('#teamjqxTree').jqxTree('checkItem', htmlElement);
					}

				});

	}, "json");

}